package com.crazy.web.service.outer.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazy.model.major.dto.ArticleDto;
import com.crazy.model.major.dto.SurveyDto;
import com.crazy.model.major.entity.TArticle;
import com.crazy.model.major.entity.TLabel;
import com.crazy.model.major.entity.TSurvey;
import com.crazy.model.major.entity.TSurveyTime;
import com.crazy.model.major.service.TSurveyService;
import com.crazy.model.major.service.TSurveyTimeService;
import com.crazy.web.service.outer.SurveyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/12/23 16:09
 */
@Service
public class SurveyServiceImpl implements SurveyService {

    @Autowired
    private TSurveyService tSurveyService;
    @Autowired
    private TSurveyTimeService tSurveyTimeService;

    @Override
    public boolean checkTelephone(String telephone) {
        QueryWrapper<TSurvey> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TSurvey.Columns.telephone, telephone);
        List<TSurvey> surveyList = tSurveyService.list(queryWrapper);
        if(surveyList!=null && surveyList.size()>0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public void add(TSurvey survey) {
        //昵称，性别，爱好不能为空，手机号符合正则表达式，且不重复
        Validator.validateNotEmpty(survey.getName(), "昵称不能为空");
        Validator.validateNotEmpty(survey.getSex(), "性别不能为空");
        Validator.validateNotEmpty(survey.getHobbys(), "爱好不能为空");
        Validator.validateMobile(survey.getTelephone(), "请填写符合规范的手机号");
        if(!checkTelephone(survey.getTelephone())){
            throw new ValidateException("该手机号已存在，请重新输入");
        }
        survey.setCreateTime(LocalDateTime.now());
        tSurveyService.save(survey);
    }

    @Override
    public IPage<TSurvey> list(long page, long rows, String search) {
        QueryWrapper<TSurvey> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(search)){
            queryWrapper.like(TSurvey.Columns.name, search);
        }
        queryWrapper.orderByDesc(TArticle.Columns.createTime);
        Page<TSurvey> surveyPage = new Page<TSurvey>(page, rows);
        IPage<TSurvey> surveyIPage = tSurveyService.page(surveyPage, queryWrapper);

        List<TSurvey> surveyList = surveyIPage.getRecords();
        List surveyDtos = new ArrayList();
        for(TSurvey survey : surveyList){
            SurveyDto surveyDto = new SurveyDto();
            BeanUtil.copyProperties(survey, surveyDto);
            surveyDtos.add(surveyDto);
        }
        surveyIPage.setRecords(surveyDtos);

        return surveyIPage;
    }

    @Override
    public void saveSurveyTime(TSurveyTime tSurveyTime) {
        if(tSurveyTime.getStartTime().isAfter(tSurveyTime.getEndTime())) {
            throw new ValidateException("开始时间不能超过结束时间");
        }
        tSurveyTimeService.save(tSurveyTime);
    }
}
